#!/bin/sh

for x in $(cat /proc/cmdline); do
  case ${x} in
    m_bpp=*) export bpp=${x#*=} ;;
    hdmimode=*) export mode=${x#*=} ;;
  esac
done

common_display_setup() {
  M="0 0 $(($X - 1)) $(($Y - 1))"
  Y_VIRT=$(($Y * 2))
  fbset -fb /dev/fb0 -g $X $Y $X $Y_VIRT $bpp
  fbset -fb /dev/fb1 -g 32 32 32 32 32
  echo $mode > /sys/class/display/mode
  echo 0 > /sys/class/graphics/fb0/free_scale
  echo 1 > /sys/class/graphics/fb0/freescale_mode
  echo $M > /sys/class/graphics/fb0/free_scale_axis
  echo $M > /sys/class/graphics/fb0/window_axis
  echo 0 > /sys/class/graphics/fb1/free_scale
  echo 1 > /sys/class/video/disable_video

  if [ "$bpp" = "32" ]; then
    echo d01068b4 0x7fc0 > /sys/kernel/debug/aml_reg/paddr
  fi
}

case $mode in
  480*)            export X=720  Y=480  ;;
  576*)            export X=720  Y=576  ;;
  720p*)           export X=1280 Y=720  ;;
  1080*)           export X=1920 Y=1080 ;;
  2160p*)          export X=3840 Y=2160 ;;
  smpte24hz*)      export X=3840 Y=2160 ;;
  640x480p60hz*)   export X=640  Y=480  ;;
  800x480p60hz*)   export X=800  Y=480  ;;
  800x600p60hz*)   export X=800  Y=600  ;;
  1024x600p60hz*)  export X=1024 Y=600  ;;
  1024x768p60hz*)  export X=1024 Y=768  ;;
  1280x800p60hz*)  export X=1280 Y=800  ;;
  1280x1024p60hz*) export X=1280 Y=1024 ;;
  1360x768p60hz*)  export X=1360 Y=768  ;;
  1366x768p60hz*)  export X=1366 Y=768  ;;
  1440x900p60hz*)  export X=1440 Y=900  ;;
  1600x900p60hz*)  export X=1600 Y=900  ;;
  1680x1050p60hz*) export X=1680 Y=1050 ;;
  1920x1200p60hz*) export X=1920 Y=1200 ;;
  2560x1080p60hz*) export X=2560 Y=1080 ;;
  2560x1440p60hz*) export X=2560 Y=1440 ;;
  2560x1600p60hz*) export X=1920 Y=1600 ;;
esac

common_display_setup

# Console unblack
echo 0 > /sys/class/graphics/fb0/blank
echo 1 > /sys/class/graphics/fb1/blank
